extends layout

append style
  link(rel="stylesheet", href="/docs/css/inlinecpc.css")
  script(type="text/javascript" src="/docs/js/native.js")

block content
  :markdown
    ## Plugins

    <script>
      _native.init("CK7DT53U",{
        targetClass: 'native-inline'
      });
    </script>

    <div class="native-inline">
      <a href="#native_link#"><span class="sponsor">Sponsor</span> #native_company# — #native_desc#</a>
    </div>

    Schemas are pluggable, that is, they allow for applying pre-packaged capabilities to extend their functionality. This is a very powerful feature.

    Suppose that we have several collections in our database and want to add last-modified functionality to each one. With plugins this is easy. Just create a plugin once and apply it to each `Schema`:

    ```javascript
    // lastMod.js
    module.exports = exports = function lastModifiedPlugin (schema, options) {
      schema.add({ lastMod: Date });

      schema.pre('save', function (next) {
        this.lastMod = new Date();
        next();
      });

      if (options && options.index) {
        schema.path('lastMod').index(options.index);
      }
    }

    // game-schema.js
    var lastMod = require('./lastMod');
    var Game = new Schema({ ... });
    Game.plugin(lastMod, { index: true });

    // player-schema.js
    var lastMod = require('./lastMod');
    var Player = new Schema({ ... });
    Player.plugin(lastMod);
    ```

    We just added last-modified behavior to both our `Game` and `Player` schemas and declared an index on the `lastMod` path of our Games to boot. Not bad for a few lines of code.

    <h3 id="global"><a href="#global">Global Plugins</a></h3>

    Want to register a plugin for all schemas? The mongoose singleton has a
    `.plugin()` function that registers a plugin for every schema. For
    example:

    ```javascript
    var mongoose = require('mongoose');
    mongoose.plugin(require('./lastMod'));

    var gameSchema = new Schema({ ... });
    var playerSchema = new Schema({ ... });
    // `lastModifiedPlugin` gets attached to both schemas
    var Game = mongoose.model('Game', gameSchema);
    var Player = mongoose.model('Player', playerSchema);
    ```

    <h3 id="official"><a href="#official">Officially Supported Plugins</a></h3>

    The Mongoose team maintains several plugins that add cool new features to
    Mongoose. Here's a few:

    * [mongoose-autopopulate](http://plugins.mongoosejs.io/plugins/autopopulate): Always [`populate()`](/docs/populate.html) certain fields in your Mongoose schemas.
    * [mongoose-lean-virtuals](http://plugins.mongoosejs.io/plugins/lean-virtuals): Attach virtuals to the results of Mongoose queries when using [`.lean()`](/docs/api.html#query_Query-lean).

    ### Community!

    Not only can you re-use schema functionality in your own projects, but you
    also reap the benefits of the Mongoose community as well. Any plugin
    published to [npm](https://npmjs.org/) and
    [tagged](https://npmjs.org/doc/tag.html) with `mongoose` will show up on
    our [search results](http://plugins.mongoosejs.io) page.
